import subprocess
import datetime
from os import path, mkdir
from time import ctime

from config import settings


def appium_start(host, port):
    bootstrap_port = str(port + 1)
    cmd = 'start /b appium -a ' + host + ' -p ' + str(port) + ' -bp ' + str(bootstrap_port) + ' --session-override'

    print('%s at %s' % (cmd, ctime()))
    # /b 后台运行并传输数据到log
    appium_log_path = settings.APPIUM_LOG_PATH
    if not path.exists(appium_log_path):
        mkdir(appium_log_path)
    current_date = datetime.datetime.now().strftime('%Y-%m-%d')
    log_file = path.join(appium_log_path, f'{current_date}_{port}.log')
    subprocess.Popen(cmd, shell=True, stdout=open(log_file, 'a'), stderr=subprocess.STDOUT)


if __name__ == '__main__':
    host = '127.0.0.1'
    port = 4723
    appium_start(host, port)
    # for i in range(2):
    #     port=4723+2*i
    #     appium_start(host,port)
    print(1)
